<?php
use Nette\Application\UI,
    Nette\Templating\Template,
    Addons\Tabella,
    Nette\Security as NS;
/**
 * Description of AdminPresenter
 *
 * @author buddha
 */
class AdminPresenter extends BasePresenter {

    /**
     * (non-phpDoc)
     *
     * @see Nette\Application\Presenter#startup()
     */
        protected function startup()
	{
                //$this->logger = NEnvironment::getService('Nette\Logger');
                //$logger = Environment::getService('Nette\Logger');
		// user authentication
                //$user = $this->context->facebook->getUser();
		//$user = $this->getUser();
		if (!$this->getUser()->isLoggedIn()) // AND !$this->context->facebook->getUser()->isLoggedIn())
		{
			$backlink = $this->getApplication()->storeRequest();
			$this->redirect('Sign:in', array('backlink' => $backlink));
		}
		parent::startup();
	}
        
        public function actionDefault()
        {

        }


        /*public function templatePrepareFilters($template)
        {
            parent::templatePrepareFilters($template);

            $texy = new Texy();
            $template->registerFilter(callback($texy, 'process'));
        }*/

        /*public function templatePrepareFilters($template)
        {
            parent::templatePrepareFilters($template);

            $texy = new Texy();
            $template->registerFilter(callback($texy, 'process'));
        }*/
        public function renderPiskoviste()
        {

           /* $template = new Template;
            // předáme šabloně parametry:
            $template->setSource('
                            Texyla
                ======

                Druhá základní možnost nastavení Texyly je **"forum"**.

                V nastavení **"forum"** nefungují například:
                - nadpisy,
                - html kód (kromě i, b, strong, ...), ostatní vložený HTML kód je chápán jen jako text a nebude vykonán

                Lze vkládat zdrojový kód. Pokud máte nainstalováno FSHL v adresáři ./texyla/fshl/ bude kód obarven (pozor na syntaxi) a odkazy na stránky.

                Smajlíky
                =======');
            //$template->registerFilter(templatePrepareFilters($template));
            //$template->render();
            //$this->template->tem=$template;
            //$this->template->texx=$tex;
            /*$i=0;
            $media=array();
            $dir = opendir( WWW_DIR."/DOWNE/");
            //\Nette\Diagnostics\Debugger::dump($dir);
            while($file = readdir($dir)) {
                //\Nette\Diagnostics\Debugger::dump($file);
                $kontrola==explode(".",$file);
                if($kontrola[1]=="avi" OR $kontrola[1]=="")
                {
                    $media[$i]=$file;
                    $i++;
                }
              }
              closedir($dir);
              $this->template->mmedia=$media;*/
        }
        public function renderCleni()
        {
            
        }

        protected function createComponentTabulkaCleni() {
            /*,'filter' => array(
                                            'kdy'=> date("Y-m-d"),'nick' => "Buddha"
                                            )*/
		$grid = new Tabella( $this->getModels()->cleni->getCleni(), array(
                                            'order' => "id",'limit'=> '25'
                                        )
                                    );
                /*	id Index
                 * nick Přezdívka
                 * jmeno Jméno
                 * prijmeni Přijmení
                 * oddil Oddíl (pov_oddily.id)
                 * druzina
                 * fce Fce/hodnosti ve středisku (pov_fce_hodnosti.id)
                 * foto xichtík
                 * aktivni Uzčuje zda se bude zobrazovat na
                 *
                 */
		$grid->addColumn('Id', 'id', array('width' => 10));
		$grid->addColumn('Jméno', 'jmeno', array('width' => 40));
		$grid->addColumn('Přijmení', 'prijmeni', array('width' => 40));
		$grid->addColumn('Přezdívka', 'nick', array('width' => 40));
		$grid->addColumn('Oddiíl', 'oddil', array('width' => 10));
		$grid->addColumn('Druzina', 'druzina', array('width' => 5));
		$grid->addColumn('Fce', 'fce', array('width' => 5));
		$grid->addColumn('Fotka', 'foto', array('width' => 30));
		$grid->addColumn('Aktivní?', 'aktivni', array('width' => 5));
		//$grid->addColumn('Kdy', 'kdy', array('width' => 200));
                return $grid;
	}
        protected function createComponentLogTabella() {
            /*,'filter' => array(
                                            'kdy'=> date("Y-m-d"),'nick' => "Buddha"
                                            )*/
		$grid = new Tabella( $this->getModels()->logovani->getLog(), array(
                                            'order' => "id", 'limit'=> '15'
                                        )
                                    );
		$grid->addColumn('Id', 'id', array('width' => 5));
		$grid->addColumn('Kdo', 'nick', array('width' => 30,'filter' => true));
		$grid->addColumn('Kde', 'kde', array('width' => 40,'filter' => true));
		$grid->addColumn('Co', 'co', array('width' => 300));
		$grid->addColumn('Kdy', 'kdy', array('width' => 50,'type' => Tabella::DATE,'filter' => true));
		//$grid->addColumn('Kdy', 'kdy', array('width' => 200));
                return $grid;
	}

        public function renderDefault()
        {

        }
        public function renderFotky()
        {
            $this->getModels()->Logovani->addLog($this->user->identity->getId(),"admin/fotky","open");//$kdo,$kde,$co,$coid= null

            /*$googleLogin = GoogleOpenID::getAssociationHandle();
            if(!$googleLogin->success()){
                $googleLogin = GoogleOpenID::createRequest("/admin/GoogleReturn");
                $googleLogin->redirect();
            }
            $googleGateway = GoogleOpenID::createRequest("#", "skautpovoden", true);
            //$googleLogin = GoogleOpenID::getResponse($googleGateway);
            if($googleGateway->success())
            {
                $user_id = $googleLogin->identity();
            }*/
        }

        public function actionGoogleLoginNow()
        {
            /*$googleLogin = GoogleOpenID::getAssociationHandle();
            $googleLogin = GoogleOpenID::createRequest("#","skautpovoden",true);
            $this->redirect("Admin:fotky");*
            //require_once "/povoden/libs/google-client/apiClient.php";

            $client = new apiClient();
            $client->discover('plus');
            $client->setScopes(array('https://www.googleapis.com/auth/plus.me'));

            if (isset($_GET['logout'])) {
              unset($_SESSION['token']);
            }

            if (isset($_GET['code'])) {
              $client->authenticate();
              $_SESSION['token'] = $client->getAccessToken();
              $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
              header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
            }

            if (isset($_SESSION['token'])) {
              $client->setAccessToken($_SESSION['token']);
              $ret = apiBatch::execute(
                $client->plus->activities->list(array('userId' => 'me', 'collection' => 'public'), 'listActivities'),
                $client->plus->people->get(array('userId' => 'me'), 'getPerson')
              );

              print "<pre>" . filter_var(print_r($ret, true), FILTER_SANITIZE_STRING) . "</pre>";
            } else {
              $client->authenticate();
            }*/

            $this->redirect("Admin:fotky");

        }
        public function actionGoogleReturn($return)
        {
            //$googleLogin = GoogleOpenID::getResponse();
            if($googleLogin->success())
            {
                $user_id = $googleLogin->identity();
            }
            $this->redirect("Admin/fotky");

        }

        protected function createComponentNewTasklistForm()
        {
            if (!$this->getUser()->isLoggedIn()) {
                throw new Nette\Application\ForbiddenRequestException();
            }

            $form = new Form();
            // ...
        }
        public function signPrihlasSeGoogle()
	{
		$form = new UI\Form;
                $form->addHidden('Passwd')
                        ->setValue('antihmota');
                $form->addHidden('Email')
                        ->setValue('skautpovoden');
                $form->setAction('https://accounts.google.com/ServiceLoginAuth');
                $form->setMethod('POST');
                $form->action;
                $this->redirect("Admin:fotky");


	}

        	/**
	 * Example form factory
	 * @return Form
	 */
	protected function createComponentExampleForm()
	{
		$form = new Nette\Application\UI\Form;

		$form->addTextarea("text", "Text", 110, 20)
			->getControlPrototype()->class("texyla");

		$form->addSubmit("s", "Submit");
		$form->onSuccess[] = callback($this, 'exampleFormSubmitted');
		return $form;
	}


	public function exampleFormSubmitted($form)
	{
		// Get values from the form
		$values = $form->getValues();

		// Get TEXY service to apply it at raw data
	    $texy = $this->getService('Texy');

		// Process data through TEXY
		$html = $texy->process($values->text);

		// Here should be action you would like to do with data - save to the DB, etc.

		// Flash message
		$this->flashMessage('Data were sucessfuly procesed');

		// Call redirect
		$this->redirect('Homepage:default');
	}


}